<div class="row">
    <div id="breadcrumb" class="col-md-12">
        <ol class="breadcrumb">
            <li><a href="#">物业管理</a></li>
            <li><a href="#">增加楼栋</a></li>
        </ol>
    </div>
</div>
<div class="row">
    <div class="col-xs-12 col-sm-12">
        <div class="box">
            <div class="box-header">
                <div class="box-icons">
                    <a class="expand-link">
                        <i class="fa fa-expand"></i>
                    </a>
                </div>
                <div class="no-move"></div>
            </div>
            <div class="box-content">
                <form id="addBuildingForm" method="post" class="form-horizontal">
                    <fieldset>
                        <legend>增加楼栋</legend>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">选择园区</label>
                            <div class="col-sm-8">
                                <select class="form-control" name="villageId" id="villageList"></select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">输入楼栋名称</label>
                            <div class="col-sm-8">
                                <input type="text" class="form-control" name="buildingName"/>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">输入楼栋编号</label>
                            <div class="col-sm-8">
                                <input type="text" class="form-control" name="buildingCode"/>
                            </div>
                        </div>
                        <div class="form-group has-feedback">
                            <label class="col-sm-2 control-label">输入描述(可选)</label>
                            <div class="col-sm-8">
                                <textarea name="description" class="form-control" id="description" rows="3"></textarea>
                            </div>
                        </div>
                    </fieldset>
                    <div class="form-group">
                        <div class="col-sm-9 col-sm-offset-2">
                            <button type="submit" class="btn btn-primary">提交</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">

    function submitForm()
    {
        var data=$("#addBuildingForm").serializeArray();
        console.log(data);
        $.ajax({
            type: "post",
            async: true,
            url: urlConfig.addBuilding,
            data: data,
            dataType: "json", //返回数据形式为json
            success: function (result) {
                if (result.status) {
                    alert("添加成功");
                }
                else
                {
                    alert(result.errorMsg.description);
                }
            },
            error: function () {
                alert("服务器连接失败,请重试!");
            }
        });
    }

    function buildingAddValidator() {
        $('#addBuildingForm').bootstrapValidator({
            message: '存在非法输入',
            fields: {
                buildingName: {
                    validators: {
                        notEmpty: {
                            message: '楼栋名称不能为空'
                        }
                    }
                },
                buildingCode: {
                    validators: {
                        notEmpty: {
                            message: '楼栋编号不能为空'
                        },
                        regexp: {
                            regexp: /^[0-9A-Za-z]+$/,
                            message: '楼栋编号只能为数字和字母的组合'
                        }
                    }
                }
            }
        }).on("success.form.bv",function(e){
            e.preventDefault();
            submitForm();
        });
    }

    /**
     * 初始化园区选择
     */
    function initVillageSelect()
    {
        $.ajax({
            type: "post",
            async: true,
            url: urlConfig.villageList,
            dataType: "json", //返回数据形式为json
            success: function (result) {
                if (result.status) {
                    var options="";
                    for(var i in result.jsonString)
                    {
                        console.log(result.jsonString[i]);
                        options+="<option value='"+result.jsonString[i].id+"'>"+result.jsonString[i].text+"</option>";
                    }
                    $("#villageList").append(options);
                }
                else
                {
                    alert(result.errorMsg.description);
                }
            },
            error: function () {
                alert("服务器连接失败,请重试!");
            }
        });
    }

    $(document).ready(function () {
        initVillageSelect();
        LoadBootstrapValidatorScript(buildingAddValidator);
    });
</script>
